package offer;

public interface IsLongPressedName {
    boolean isLongPressedName(String name, String typed);
}

class IsLongPressedNameTest {
    static final String[] TEST_01 = new String[]{"pyplrz", "ppyypllr"};
    static final String[] TEST_02 = new String[]{"alex","aaleex"};
    public static void main(String[] args) {
        new IsLongPressedNameImpl1().isLongPressedName(TEST_01[0], TEST_01[1]);

    }
}

class IsLongPressedNameImpl1 implements IsLongPressedName  {
    @Override
    public boolean isLongPressedName(String name, String typed) {
        if(name.length() > typed.length()){
            return false;
        }else if(name.length() == typed.length()){
            return name.equals(typed);
        }else{
            int ptrType = 0;
            int i;
            for (i = 0; i < name.length() - 1; i++) {
                char c = name.charAt(i);
                if(c != name.charAt(i + 1)){
                    // 允许重复
                    if(c == typed.charAt(ptrType)){
                        // 过滤掉type中重复元素
                        while(ptrType < typed.length() && typed.charAt(ptrType) == c){
                            ptrType++;
                        }
                    }else{
                        // 不匹配
                        return false;
                    }
                }else{
                    // name-> aa
                    if(c == typed.charAt(ptrType)){
                        ptrType++;
                    }else{
                        return false;
                    }
                }
            }

            char lastChar = name.charAt(name.length() - 1);
            if(ptrType < typed.length()){
                if(lastChar == typed.charAt(ptrType)){
                    while( ptrType < typed.length() && lastChar == typed.charAt(ptrType) ){
                        ptrType++;
                    }
                    if(ptrType == typed.length()){
                        return true;
                    }else {
                        return false;
                    }
                }else {
                    return false;
                }
            }else{
                return false;
            }

        }

    }
}